using System.Collections.Generic;
using UnityEngine;


namespace UFrame
{
    /// <summary>
    /// 简单类型的转换
    /// </summary>
    public class TypeConvert
    {
        /// <summary>
        /// 将字符串转换为一个整形数组
        /// </summary>
        /// <returns>转换成的数组</returns>
        /// <param name="value">需要转换的字符串</param>
        public static int[] StringConvertInt(string value)
        {
            string[] s = value.Split(',');
            int[] it = new int[s.Length];
            for (int i = 0; i < it.Length; ++i)
            {
                int.TryParse(s[i], out it[i]);
            }

            return it;
        }

        /// <summary>
        /// 将字符串转换为Vector3向量
        /// </summary>
        /// <returns>返回转换的Vector3向量</returns>
        /// <param name="value">需要转换的字符串</param>
        public static Vector3 StringConvertVector3(string value)
        {
            string[] s = value.Split(',');
            Vector3 vector = Vector3.zero;
            float.TryParse(s.Length > 0 ? s[0] : "", out vector.x);
            float.TryParse(s.Length > 1 ? s[1] : "", out vector.y);
            float.TryParse(s.Length > 2 ? s[2] : "", out vector.z);
            return vector;
        }

        /// <summary>
        /// 将一个三维向量转换为字符串
        /// </summary>
        /// <returns>返回字符串</returns>
        /// <param name="value">参数三维向量</param>
        public static string Vector3ConvertString(Vector3 value)
        {
            return value.x.ToString() + "," + value.y.ToString() + "," + value.z.ToString();
        }

        /// <summary>
        /// 将字符串转换为一个四元数
        /// </summary>
        /// <returns>返回转换成的四元数</returns>
        /// <param name="value">要转换的字符串</param>
        public static Quaternion StringConvertQuaternion(string value)
        {
            string[] s = value.Split(',');
            Quaternion quaternion = new Quaternion();
            float.TryParse(s.Length > 0 ? s[0] : "", out quaternion.x);
            float.TryParse(s.Length > 1 ? s[1] : "", out quaternion.y);
            float.TryParse(s.Length > 2 ? s[2] : "", out quaternion.z);
            float.TryParse(s.Length > 3 ? s[3] : "", out quaternion.w);
            return quaternion;
        }

        /// <summary>
        /// 将四元数转换为字符串
        /// </summary>
        /// <returns>返回转换成的字符串</returns>
        /// <param name="value">要转换的四元数</param>
        public static string QuaternionConvertString(Quaternion value)
        {
            return value.x.ToString() + "," + value.y.ToString() + "," + value.z.ToString() + "," + value.w.ToString();
        }


        public static byte[] IntToBytes(int value)
        {
            byte[] src = new byte[4];
            src[0] = (byte)((value >> 24) & 0xFF);
            src[1] = (byte)((value >> 16) & 0xFF);
            src[2] = (byte)((value >> 8) & 0xFF);
            src[3] = (byte)(value & 0xFF);
            return src;
        }

        public static byte[] ShortToBytes(short value)
        {
            byte[] src = new byte[2];

            src[0] = (byte)((value >> 8) & 0xFF);
            src[1] = (byte)(value & 0xFF);
            return src;
        }

        public static short BytesToShort(byte[] values)
        {
            return (short)(values[1] | (values[0] << 8));
        }

        public static int BytesToInt(byte[] values)
        {
            return values[0] | values[1] << 8 | values[2] << 16 | values[3] << 24;
        }

        public static short BytesToShort(byte[] values, int offer)
        {
            return (short)(values[offer + 1] | (values[offer] << 8));
        }

        public static int BytesToInt(byte[] values, int offer)
        {
            return values[offer + 3] | values[offer + 2] << 8 | values[offer + 1] << 16 |
                           values[offer] << 24;
        }


        #region 转换字符串成数组 和 List

        public string[] StringAry(string a)
        {
            string[] ary = new string[1];
            ary[0] = a;
            return ary;
        }

        public string[] StringAry(string a, string b)
        {
            string[] ary = new string[2];
            ary[0] = a;
            ary[1] = b;
            return ary;
        }

        public string[] StringAry(string a, string b, string c)
        {
            string[] ary = new string[3];
            ary[0] = a;
            ary[1] = b;
            ary[2] = c;
            return ary;
        }

        public string[] StringAry(string a, string b, string c, string d)
        {
            string[] ary = new string[4];
            ary[0] = a;
            ary[1] = b;
            ary[2] = c;
            ary[3] = d;
            return ary;
        }

        public List<string> StringList(string a)
        {
            List<string> ary = new List<string>();
            ary.Add(a);
            return ary;
        }

        public List<string> StringList(string a, string b)
        {
            List<string> ary = new List<string>();
            ary.Add(a);
            ary.Add(b);
            return ary;
        }

        public List<string> StringList(string a, string b, string c)
        {
            List<string> ary = new List<string>();
            ary.Add(a);
            ary.Add(b);
            ary.Add(c);
            return ary;
        }

        public List<string> StringList(string a, string b, string c, string d)
        {
            List<string> ary = new List<string>();
            ary.Add(a);
            ary.Add(b);
            ary.Add(c);
            ary.Add(d);
            return ary;
        }

        #endregion
    }
}